//
//  XHNavigationBar.m
//  下拉图片放大
//
//  Created by zxy on 2019/5/23.
//  Copyright © 2019 zxy. All rights reserved.
//

#import "XHNavigationBar.h"
#import "UIView+XMGExtension.h"
@implementation XHNavigationBar

- (instancetype)initWithFrame:(CGRect)frame
                leftItemImageName:(NSString *)leftItemImgName
                title:(NSString *)title
                rightItemImageName:(NSString *)rightItemImgName
                delegate:(id<XHNavigationBarDelegate>)delegate
               {
    self = [super initWithFrame:frame];
    if(self){
        self.delegate = delegate;
        UIImageView *leftItem = [[UIImageView alloc]initWithFrame:CGRectMake(15, 35, 0, 0)];
        leftItem.image = [UIImage imageNamed:leftItemImgName];
        [leftItem sizeToFit];
        leftItem.userInteractionEnabled = YES;
        [self addSubview:leftItem];
        
        //添加手势
        [leftItem addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(LeftItemClick)]];
        
        UILabel *titleLab = [[UILabel alloc]init];
        titleLab.text = title;
        titleLab.textColor = [UIColor whiteColor];
        titleLab.font = [UIFont systemFontOfSize:16 weight:UIFontWeightBold];
        [titleLab sizeToFit];
        titleLab.xmg_centerX = self.xmg_centerX;
        titleLab.xmg_centerY = leftItem.xmg_centerY;
        [self addSubview:titleLab];
        
        if(rightItemImgName != nil){
            UIImageView *rightItem = [[UIImageView alloc]init];
            rightItem.image = [UIImage imageNamed:rightItemImgName];
            [rightItem sizeToFit];
            rightItem.xmg_x = [UIScreen mainScreen].bounds.size.width - rightItem.frame.size.width - 15;
            rightItem.xmg_centerY = leftItem.xmg_centerY;
            [self addSubview:rightItem];
            rightItem.userInteractionEnabled = YES;
            [rightItem addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(rightItemClick)]];
        }
    }
    return self;
}

-(void)LeftItemClick{
    if(_delegate && [_delegate respondsToSelector:@selector(leftItemAction)]){
        [_delegate leftItemAction];
    }
}


-(void)rightItemClick{
    if(_delegate && [_delegate respondsToSelector:@selector(rightItemAction)]){
        [_delegate rightItemAction];
    }
}
@end
